<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { assignRoles } from '@/api/employee'
import { getUserDetailById } from '@/api/user'
import { getRoles } from '@/api/settings'
export default {
  props: {
    roleid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 存储保存的角色权限
      list: []// 渲染权限数组
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 获取所有角色列表
    async loadRoles() {
      const res = await getRoles()
      console.log('所有角色列表', res)
      this.list = res.data.rows
      // console.log('loadRoles...........', res)
      const info = await getUserDetailById(this.roleid)
      console.log('getUserDetailById...........', info)
      // 保存这个员工当前的已经有的角色
      this.roleIds = info.data.roleIds
    },
    // 关闭弹层
    closeDialog() {
      this.$emit('close')
    },
    // 保存当前角色信息
    async hSubmit() {
      const res = await assignRoles({ id: this.roleid, roleIds: this.roleIds })
      console.log('保存角色', res)
      this.$emit('close')
    }
  }
}
</script>
